/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.siglo21.ppo.view;

import ar.com.siglo21.ppo.controller.ControladorPrestamo;
import ar.com.siglo21.ppo.model.Prestamo;
import ar.com.siglo21.ppo.util.TrabajoIntegradorException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class PrestamoView extends javax.swing.JFrame {
    
    
    private ControladorPrestamo controladorPrestamo;
    
            

    /**
     * Creates new form AutorView
     */
    public PrestamoView() {
        
        controladorPrestamo = new ControladorPrestamo();
        initComponents();
        cargarPrestamos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListPrestamo = new javax.swing.JList();
        jButtonNuevoPrestamo = new javax.swing.JButton();
        jButtonModificarPrestamo = new javax.swing.JButton();
        jButtonEliminarPrestamo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("PRESTAMO");

        jListPrestamo.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jListPrestamo);

        jButtonNuevoPrestamo.setText("Nuevo");
        jButtonNuevoPrestamo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoPrestamoActionPerformed(evt);
            }
        });

        jButtonModificarPrestamo.setText("Modificar");
        jButtonModificarPrestamo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonModificarPrestamoActionPerformed(evt);
            }
        });

        jButtonEliminarPrestamo.setText("Eliminar");
        jButtonEliminarPrestamo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarPrestamoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(228, 228, 228)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(jButtonNuevoPrestamo)
                        .addGap(43, 43, 43)
                        .addComponent(jButtonModificarPrestamo)
                        .addGap(36, 36, 36)
                        .addComponent(jButtonEliminarPrestamo)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 54, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(44, 44, 44)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonNuevoPrestamo)
                    .addComponent(jButtonModificarPrestamo)
                    .addComponent(jButtonEliminarPrestamo))
                .addGap(38, 38, 38))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEliminarPrestamoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarPrestamoActionPerformed
          Prestamo prestamo = (Prestamo) jListPrestamo.getSelectedValue();
        if(prestamo != null){
            JOptionPane.showMessageDialog(this, "Proximamente disponible: eliminar prestamos", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(this, "Proximamente disponible: eliminar prestamos", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButtonEliminarPrestamoActionPerformed

    private void jButtonNuevoPrestamoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoPrestamoActionPerformed
         Parametros parametros = new Parametros() {
            @Override
            public void datosActualizados() {
                cargarPrestamos();
                JOptionPane.showMessageDialog(PrestamoView.this, "Prestamo agregado con exito", "Informacion", JOptionPane.INFORMATION_MESSAGE);
            }
        };
        new DatosPrestamoView(parametros).setVisible(true);
    }//GEN-LAST:event_jButtonNuevoPrestamoActionPerformed

    private void jButtonModificarPrestamoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModificarPrestamoActionPerformed
         Prestamo prestamo = (Prestamo) jListPrestamo.getSelectedValue();
         if(prestamo != null){
             Parametros parametros = new Parametros() {
                 @Override
                 public void datosActualizados() {
                     cargarPrestamos();
                     JOptionPane.showMessageDialog(PrestamoView.this, "Prestamo actualizado con exito", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                 }
             };
             new DatosPrestamoView(prestamo, parametros).setVisible(true);
         }
    }//GEN-LAST:event_jButtonModificarPrestamoActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEliminarPrestamo;
    private javax.swing.JButton jButtonModificarPrestamo;
    private javax.swing.JButton jButtonNuevoPrestamo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jListPrestamo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void cargarPrestamos() {
        try {
            List<Prestamo> listaPrestamos = controladorPrestamo.cargarPrestamos();
            jListPrestamo.setListData(listaPrestamos.toArray(new Prestamo[]{}));
        } catch (SQLException | TrabajoIntegradorException ex) {
            Logger.getLogger(GeneroView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
